package com.key.controller;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.key.model.Premission;
import com.key.tools.ConnectionPool;

@Controller
@RequestMapping("/auth")
public class PremissionController {
	@RequestMapping("/query")
	public ModelAndView query(Premission auth, HttpServletRequest request) {
		ConnectionPool cp = ConnectionPool.getInstance();
		Connection ct = cp.getConnection();
		List<Premission> list = new ArrayList<Premission>();
		try {
			int index = auth.getPageNum();
			int number = auth.getEveryPage();
			int startindex = (index - 1) * number;
			String sql = "select * from t_premission p left join t_group g on p.group_id = g.id where module like '%"
					+ auth.getModule()
					+ "%' and action like '%"
					+ auth.getAction() + "%'";
			if (!auth.getGroupId().equals("")
					&& !auth.getGroupName().equals("")) {
				sql += " and group_id = '" + auth.getGroupId() + "'";
			}
			sql += "  limit ?,?";
			PreparedStatement ps = ct.prepareStatement(sql);
			ps.setInt(1, startindex);
			ps.setInt(2, number);
			ResultSet rs = ps.executeQuery();
			while (rs.next()) {
				Premission p = new Premission();
				p.setAction(rs.getString("action"));
				p.setGroupId(rs.getString("group_id"));
				p.setGroupName(rs.getString("groupname"));
				p.setId(rs.getString("id"));
				p.setModule(rs.getString("module"));
				list.add(p);
			}

			sql = "select count(*) as cc from t_premission p left join t_group g on p.group_id = g.id where module like '%"
					+ auth.getModule()
					+ "%' and action like '%"
					+ auth.getAction() + "%'";
			if (!auth.getGroupId().equals("")
					&& !auth.getGroupName().equals("")) {
				sql += " and group_id = '" + auth.getGroupId() + "'";
			}
			PreparedStatement ps2 = ct.prepareStatement(sql);
			ResultSet rs2 = ps2.executeQuery();
			if (rs2.next()) {
				auth.setTotalCount(rs2.getInt("cc"));
			}

			request.setAttribute("auth", auth);
			request.setAttribute("list", list);
			rs.close();
			ps.close();
		} catch (Exception e) {
		} finally {
			try {
				ct.close();
			} catch (Exception e) {
			}
		}
		return new ModelAndView("jsp/auth/list");
	}

	@RequestMapping("/new")
	public ModelAndView newAuth(Premission auth, HttpServletRequest request) {
		ConnectionPool cp = ConnectionPool.getInstance();
		Connection ct = cp.getConnection();
		try {
			PreparedStatement ps = ct
					.prepareStatement("insert into t_premission(group_id,module,action) values(?,?,?)");
			ps.setString(1, auth.getGroupId());
			ps.setString(2, auth.getModule());
			ps.setString(3, auth.getAction());
			int r = ps.executeUpdate();
			if (r > 0) {
				request.setAttribute("result", true);
			} else {
				request.setAttribute("result", false);
				request.setAttribute("errorMessage", "");
			}
			ps.close();
		} catch (Exception e) {
			request.setAttribute("result", false);
			request.setAttribute("errorMessage", e.getMessage());
		} finally {
			try {
				ct.close();
			} catch (Exception e) {
				request.setAttribute("result", false);
				request.setAttribute("errorMessage", e.getMessage());
			}
		}
		return new ModelAndView("/jsp/sys/result");
	}

	@RequestMapping("/delete")
	public ModelAndView deleteAuth(String id, HttpServletRequest request) {
		ConnectionPool cp = ConnectionPool.getInstance();
		Connection ct = cp.getConnection();
		try {
			PreparedStatement ps = ct
					.prepareStatement("delete from t_premission where id = ?");
			ps.setString(1, id);
			int r = ps.executeUpdate();
			if (r > 0) {
				request.setAttribute("result", true);
			} else {
				request.setAttribute("result", false);
				request.setAttribute("errorMessage", "");
			}
			ps.close();
		} catch (Exception e) {
			request.setAttribute("result", false);
			request.setAttribute("errorMessage", e.getMessage());
		} finally {
			try {
				ct.close();
			} catch (Exception e) {
				request.setAttribute("result", false);
				request.setAttribute("errorMessage", e.getMessage());
			}
		}
		return new ModelAndView("/jsp/sys/result");
	}

	@RequestMapping("/edit")
	public ModelAndView editAuth(String id, HttpServletRequest request) {
		ConnectionPool cp = ConnectionPool.getInstance();
		Connection ct = cp.getConnection();
		try {
			PreparedStatement ps = ct
					.prepareStatement("select * from t_premission u left join t_group g on u.group_id = g.id where u.id = ?");
			ps.setString(1, id);
			ResultSet rs = ps.executeQuery();
			Premission p = new Premission();
			p.setId(id);
			if (rs.next()) {
				p.setAction(rs.getString("action"));
				p.setGroupId(rs.getString("group_id"));
				p.setGroupName(rs.getString("groupname"));
				p.setModule(rs.getString("module"));
			}
			request.setAttribute("auth", p);
			rs.close();
			ps.close();
		} catch (Exception e) {
		} finally {
			try {
				ct.close();
			} catch (Exception e) {
			}
		}
		return new ModelAndView("/jsp/auth/edit");
	}

	@RequestMapping("/modify")
	public ModelAndView modifyOrg(Premission auth, HttpServletRequest request) {
		ConnectionPool cp = ConnectionPool.getInstance();
		Connection ct = cp.getConnection();
		try {
			PreparedStatement ps = ct
					.prepareStatement("update t_premission set group_id = ?,module=?,action=? where id=?");
			ps.setString(1, auth.getGroupId());
			ps.setString(2, auth.getModule());
			ps.setString(3, auth.getAction());
			ps.setString(4, auth.getId());
			int r = ps.executeUpdate();
			if (r > 0) {
				request.setAttribute("result", true);
			} else {
				request.setAttribute("result", false);
				request.setAttribute("errorMessage", "");
			}
			ps.close();
		} catch (Exception e) {
			request.setAttribute("result", false);
			request.setAttribute("errorMessage", e.getMessage());
		} finally {
			try {
				ct.close();
			} catch (Exception e) {
				request.setAttribute("result", false);
				request.setAttribute("errorMessage", e.getMessage());
			}
		}
		return new ModelAndView("/jsp/sys/result");
	}
}
